from http import HTTPStatus
import dashscope
import json

with open('res\JSON\APIconfig.json', 'r') as APIconfig:
  data = json.load(APIconfig)
  dashscope.api_key = data['AliQwenConfig']['APIkey']

def call_with_messages():

    messages = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
    with open('res\cache\MsgToAliQwen.json', 'r', encoding = 'utf-8') as Msg:
        messages[0].update(json.load(Msg))
    # print(messages)

    response = dashscope.Generation.call(
        dashscope.Generation.Models.qwen_turbo,
        messages = messages,
        result_format='message',  # set the result to be "message" format.
    )

    if response.status_code == HTTPStatus.OK:
        print(response['output']['choices'][0]['message'])
    else:
        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
            response.request_id, response.status_code,
            response.code, response.message
        ))

if __name__ == '__main__':
    call_with_messages()